<template>
	<view class="content">
		<view class="row b-b">
			<text class="tit">联系人</text>
			<input class="input" type="text" v-model="addressData.name" placeholder="收货人姓名" placeholder-class="placeholder" />
		</view>
		<view class="row b-b">
			<text class="tit">手机号</text>
			<input class="input" type="number" v-restorelayui v-model="addressData.mobile" placeholder="收货人手机号码" placeholder-class="placeholder" />
		</view>
		<picker :value="value" mode="multiSelector" @change="picker" @columnchange="columnPicker" :range="multiArray">
			<view class="row b-b">
				<text class="tit">地址</text>
				<text class="input" v-if="addressData.address">{{addressData.address}}</text>
				<text class="input" v-if="!addressData.address" style="color: #808080;">选择地址</text>
				<text class="yticon icon-shouhuodizhi iconfont">&#xe622;</text>
			</view>
		</picker>
		<view class="row b-b"> 
			<text class="tit">门牌号</text>
			<input class="input" type="text" v-restorelayui v-model="addressData.area" placeholder="小区、楼号、门牌号" placeholder-class="placeholder" />
		</view>
		
		<button class="add-btn" @click="confirm">提交</button>
	</view>
</template>

<script>
	import cityData from '@/static/js/city.data.js'
	export default {
		data() {
			return {
				isShow:false,
				selectList: cityData, //接口返回picker数据,此处就直接使用本地测试数据
				multiArray: [], //picker数据
				value: [0, 0, 0],
				text: "",
				id:"",
				manageType:'add',
				addressData: {
					name: '',
					mobile: '',
					address: '',
					city_code:'',
					area: '',
				}
			}
		},
		onLoad(option){
			let title = '新增收货地址';
			if(option.type==='edit'){
				title = '编辑收货地址'
				this.addressData = JSON.parse(option.data)
				this.manageType = option.type;
			}
			uni.setNavigationBarTitle({ title });
			
			// 地址下拉数据
			if(this.addressData.city_code){
				this.setAddressDefault();
			}else{
				this.multiArray = [
					this.toArr(this.selectList),
					this.toArr(this.selectList[0].children),
					this.toArr(this.selectList[0].children[0].children)
				];
			}
		},
		methods: {
			//默认值
			setAddressDefault(){
				this.selectList.forEach((h,index)=>{
					(h.children || []).forEach((f,key)=>{
						(f.children || []).forEach((g,i)=>{
							if(this.addressData.city_code == g.code){
								this.multiArray = [
									this.toArr(this.selectList),
									this.toArr(this.selectList[index].children),
									this.toArr(this.selectList[index].children[key].children)
								];
								this.value=[index,key,i];
							}
						});
					});
				});
			},
			toArr(object) {
				let arr = [];
				for (let i in object) {
					arr.push(object[i].name);
				}
				return arr;
			},
			picker: function(e) {
				let value = e.detail.value;
				if (this.selectList.length > 0) {
					let provice = this.selectList[value[0]].name
					let city = this.selectList[value[0]].children[value[1]].name
					let district = this.selectList[value[0]].children[value[1]].children[value[2]].name
					let address = provice + city + district;
					let city_code = this.selectList[value[0]].children[value[1]].children[value[2]].code
					this.addressData.addressName = address;
					this.addressData.address = address;
					this.addressData.city_code = city_code;
				}
			},
			columnPicker: function(e) {
				//第几列 下标从0开始
				let column = e.detail.column;
				//第几行 下标从0开始
				let value = e.detail.value;
				if (column === 0) {
					this.multiArray = [
						this.multiArray[0],
						this.toArr(this.selectList[value].children),
						this.toArr(this.selectList[value].children[0].children)
					];
					this.value = [value, 0, 0]
				} else if (column === 1) {
					this.multiArray = [
						this.multiArray[0],
						this.multiArray[1],
						this.toArr(this.selectList[this.value[0]].children[value].children)
					];
					this.value = [this.value[0], value, 0]
				}
			},
			switchChange(e){
				this.addressData.default = e.detail;
			},
			//地图选择地址
			chooseLocation(){
				uni.chooseLocation({
					success: (data)=> {
						if( data.name == '我的位置'){
							setTimeout(()=>{
								this.$util.msg('不能选择 "我的位置" ');
							}, 200)
						}else{
							this.addressData.addressName = data.name;
							this.addressData.address = data.name;
						}
					}
				})
			},
			
			//提交
			confirm(){
				let data = this.addressData;
				if(!data.name){
					this.$util.msg('请填写收货人姓名');
					return;
				}
				if(!/(^1[3|4|5|7|8][0-9]{9}$)/.test(data.mobile)){
					this.$util.msg('请输入正确的手机号码');
					return;
				}
				if(!data.address){
					this.$util.msg('请选择所在位置');
					return;
				}
				if(!data.area){
					this.$util.msg('请填写门牌号信息');
					return;
				}
				
				let url = "/app/v1/Address/addAddress";
				if(this.manageType == 'edit'){
					url = "/app/v1/Address/editAddress";
				}
				this.$request(url,data).then((res)=>{
					if( this.$util.prePage() ) this.$util.prePage().refreshList(data, this.manageType);
					this.$util.msg(`地址${this.manageType=='edit' ? '修改': '添加'}成功`);
					setTimeout(()=>{ uni.navigateBack() }, 200)
				});
			},
		}
	}
</script>

<style lang="scss">
	page{
		background: $page-color-base;
	}

	.row{
		display: flex;
		align-items: center;
		position: relative;
		padding:0 30upx;
		height: 110upx;
		background: #fff;
		border-bottom: 1px solid #f8f8f8;
		
		.tit{
			flex-shrink: 0;
			width: 120upx;
			font-size: 30upx;
			color: $font-color-dark;
		}
		.input{
			flex: 1;
			font-size: 30upx;
			color: $font-color-dark;
		}
		.icon-shouhuodizhi{
			font-size: 36upx;
			color: $font-color-light;
		}
	}
	.default-row{
		margin-top: 16upx;
		.tit{
			flex: 1;
		}
		switch{
			transform: translateX(16upx) scale(.9);
		}
	}
	.add-btn{
		display: flex;
		align-items: center;
		justify-content: center;
		width: 690upx;
		height: 80upx;
		margin: 60upx auto;
		font-size: $uni-font-size-lg;
		color: #fff;
		background-color: $base-color;
		border-radius: 10upx;
		box-shadow: 1px 2px 5px rgba(219, 63, 96, 0.4);
	}
</style>
